//
//  LGTTaskListVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/16.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import ZHRefresh

class LGTTaskListVC: LGTBaseVC, UITableViewDelegate, UITableViewDataSource{
    var page:Int = 1
    var data:Array<TaskModel> = []
    var tableView:UITableView?
    var _param:Dictionary<String, Any> = [:]
    var nav:UINavigationController?
    var param:Dictionary<String, Any>{
        get{
            return _param
        }
        set{
            _param = newValue
            self.tableView?.header?.beginRefreshing()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setBlock()
    }
    
    func setBlock(){
        weak var weakSelf = self
        self.reloadBlock = {() in
            weakSelf?.tableView?.header?.beginRefreshing()
        }
    }
    
    override func setUI() {
        super.setUI()
        self.setBavBarStyle(navBarHiden: false, title: "", back: false)
        self.view.backgroundColor = ViewBackColor
        self.tableView = UITableView.init(frame: CGRect.init(x: 0, y:0, width: Double(KScreenW), height: Double(KScreenH) - TabBarH() - 45.0 - NavBarH()), style: UITableView.Style.plain)
        self.view.addSubview(self.tableView!)
        self.tableView?.backgroundColor = ViewBackColor
        self.tableView?.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.register(LGTHomeCell.classForCoder(), forCellReuseIdentifier: "cell")
        
        weak var weakSelf = self
        self.tableView?.header = ZHRefreshNormalHeader.headerWithRefreshing(block: {
            weakSelf?.page = 1
            weakSelf?.tableView?.footer?.endRefreshing()
            weakSelf?.getData()
        })
        
        self.tableView?.footer = ZHRefreshBackNormalFooter.footerWithRefreshing(block: {
            weakSelf?.page += 1
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.getData()
        })
    }

    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let detailVC:LGTPostDetailVC = LGTPostDetailVC()
        detailVC.viewDetailType = DetailType.LLZ_Task_Detail_Type
        detailVC.taskModel = self.data[indexPath.row]
        self.nav?.pushViewController(detailVC, animated: true)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:LGTHomeCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LGTHomeCell
        cell.model = self.data[indexPath.row]
        return cell
    }
    
    func getData(){
        weak var weakSelf = self
        var param:Dictionary<String, Any> = self.param
        param["page"] = self.page
        
        print(LLZ_Task_List_URL)
        print(param)
        LGTHttpUtil.httpUtil.requestData(url: LLZ_Task_List_URL, param: param, showMessage: false, success: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
            let data:Array<Dictionary<String, Any>> =  json["data"] as? Array<Dictionary<String, Any>> ?? Array.init()
            
            var modeArray:Array<TaskModel> = Array.init()
            for i in 0..<data.count{
                let param:Dictionary<String, Any> = data[i]
                let model:TaskModel = try! lgtDecode(TaskModel.self, param: param)
                modeArray.append(model)
            }
            
            if weakSelf?.page == 1{
                weakSelf?.data = modeArray
                
            }else{
                if weakSelf?.data == nil{
                    weakSelf?.data = Array.init()
                }
                weakSelf?.data.append(contentsOf: modeArray)
            }
            
            if weakSelf?.data.count == 0{
                weakSelf?.showNoData(image: NoData_Image, text: NoData_Text, subText: "", haveNav:false)
            }else{
                weakSelf?.noView?.isHidden = true
            }
            
            if modeArray.count == 0{
                weakSelf?.tableView?.footer?.endRefreshingWithNoMoreData()
            }else{
                weakSelf?.tableView?.footer?.resetNoMoreData()
            }
            weakSelf?.tableView?.reloadData()
        }, failure: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
        })
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.noView?.isHidden = true
        self.tableView?.header?.beginRefreshing()
    }
}
